package bc

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/bc"
	bcReq "github.com/flipped-aurora/gin-vue-admin/server/model/bc/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type BcRechargeApi struct {
}

var bcRechargeService = service.ServiceGroupApp.BcServiceGroup.BcRechargeService

// CreateBcRecharge 创建bcRecharge表
// @Tags BcRecharge
// @Summary 创建bcRecharge表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body bc.BcRecharge true "创建bcRecharge表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /bcRecharge/createBcRecharge [post]
func (bcRechargeApi *BcRechargeApi) CreateBcRecharge(c *gin.Context) {
	var bcRecharge bc.BcRecharge
	err := c.ShouldBindJSON(&bcRecharge)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	userData, err := userService.GetUserInfoByName(bcRecharge.SysUserName)
	if err != nil {
		global.GVA_LOG.Error("查询用户信息失败!", zap.Error(err))
		response.FailWithMessage("查询用户信息失败", c)
		return
	}

	userData.TotalAmount = (bcRecharge.RechargeNum*100 + userData.TotalAmount*100) / 100
	userData.Balance = (bcRecharge.RechargeNum*100 + userData.Balance*100) / 100
	userService.SetSelfInfo(userData)

	bcRecharge.SettleOffice = userData.SettleOffice
	if err := bcRechargeService.CreateBcRecharge(&bcRecharge); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteBcRecharge 删除bcRecharge表
// @Tags BcRecharge
// @Summary 删除bcRecharge表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body bc.BcRecharge true "删除bcRecharge表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /bcRecharge/deleteBcRecharge [delete]
func (bcRechargeApi *BcRechargeApi) DeleteBcRecharge(c *gin.Context) {
	var bcRecharge bc.BcRecharge
	err := c.ShouldBindJSON(&bcRecharge)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	//userData, err := userService.GetUserInfoByName(bcRecharge.SysUserName)
	//if err != nil {
	//	global.GVA_LOG.Error("查询用户信息失败!", zap.Error(err))
	//	response.FailWithMessage("查询用户信息失败", c)
	//	return
	//}
	//userData.TotalAmount = (userData.TotalAmount*100 - bcRecharge.RechargeNum*100) / 100
	//userData.Balance = (userData.Balance*100 - bcRecharge.RechargeNum*100) / 100
	//userService.SetSelfInfo(userData)

	if err := bcRechargeService.DeleteBcRecharge(bcRecharge); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteBcRechargeByIds 批量删除bcRecharge表
// @Tags BcRecharge
// @Summary 批量删除bcRecharge表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除bcRecharge表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /bcRecharge/deleteBcRechargeByIds [delete]
func (bcRechargeApi *BcRechargeApi) DeleteBcRechargeByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	//bcRechargeData, err := bcRechargeService.GetBcRechargeByList(IDS)
	//for i := 0; i < len(bcRechargeData); i++ {
	//	userData, err := userService.GetUserInfoByName(bcRechargeData[i].SysUserName)
	//	if err != nil {
	//		global.GVA_LOG.Error("查询用户信息失败!", zap.Error(err))
	//		response.FailWithMessage("查询用户信息失败", c)
	//		return
	//	}
	//	userData.TotalAmount = (userData.TotalAmount*100 - bcRechargeData[i].RechargeNum*100) / 100
	//	userData.Balance = (userData.Balance*100 - bcRechargeData[i].RechargeNum*100) / 100
	//	userService.SetSelfInfo(userData)
	//}

	if err := bcRechargeService.DeleteBcRechargeByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateBcRecharge 更新bcRecharge表
// @Tags BcRecharge
// @Summary 更新bcRecharge表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body bc.BcRecharge true "更新bcRecharge表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /bcRecharge/updateBcRecharge [put]
func (bcRechargeApi *BcRechargeApi) UpdateBcRecharge(c *gin.Context) {
	var bcRecharge bc.BcRecharge
	err := c.ShouldBindJSON(&bcRecharge)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	//userData, err := userService.GetUserInfoByName(bcRecharge.SysUserName)
	//if err != nil {
	//	global.GVA_LOG.Error("查询用户信息失败!", zap.Error(err))
	//	response.FailWithMessage("查询用户信息失败", c)
	//	return
	//}
	//rebcRecharge, err := bcRechargeService.GetBcRecharge(bcRecharge.ID)
	//userData.TotalAmount = (userData.TotalAmount*100 + bcRecharge.RechargeNum*100 - rebcRecharge.RechargeNum*100) / 100
	//userData.Balance = (userData.Balance*100 + bcRecharge.RechargeNum*100 - rebcRecharge.RechargeNum*100) / 100
	//userService.SetSelfInfo(userData)
	if err := bcRechargeService.UpdateBcRecharge(bcRecharge); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindBcRecharge 用id查询bcRecharge表
// @Tags BcRecharge
// @Summary 用id查询bcRecharge表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query bc.BcRecharge true "用id查询bcRecharge表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /bcRecharge/findBcRecharge [get]
func (bcRechargeApi *BcRechargeApi) FindBcRecharge(c *gin.Context) {
	var bcRecharge bc.BcRecharge
	err := c.ShouldBindQuery(&bcRecharge)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if rebcRecharge, err := bcRechargeService.GetBcRecharge(bcRecharge.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"rebcRecharge": rebcRecharge}, c)
	}
}

// GetBcRechargeList 分页获取bcRecharge表列表
// @Tags BcRecharge
// @Summary 分页获取bcRecharge表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query bcReq.BcRechargeSearch true "分页获取bcRecharge表列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /bcRecharge/getBcRechargeList [get]
func (bcRechargeApi *BcRechargeApi) GetBcRechargeList(c *gin.Context) {
	var pageInfo bcReq.BcRechargeSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	data, err := utils.GetClaims(c)
	if err != nil {
		response.FailWithMessage("获取失败", c)
		return
	}
	list := make([]string, 0)
	switch data.AuthorityId {
	case 8881:
		data1, err := userService.GetUserListByCode(data.SettleOffice)
		if err != nil {
			response.FailWithMessage("获取失败", c)
			return
		}

		for i := 0; i < len(data1); i++ {
			list = append(list, data1[i].Username)
		}
	case 1:
		list = append(list, data.Username)
	}

	if list, total, err := bcRechargeService.GetBcRechargeInfoList(pageInfo, list); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
